
/** @odoo-module **/
const {
    markup,
    Component,
    onWillStart,
    useEnv,
    useSubEnv,
    useChildSubEnv,
    useRef,
    useState,
    mount,
    xml,
    whenReady,
    onMounted,
    onError,
    loadFile,
    onWillUpdateProps,
    useEffect,
    useComponent
} = owl;
class FreqModal extends Component {
    static template = xml`
     <div class="modal fade" id="freqModal">
  <div class="modal-dialog  modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5">【<t t-esc="state.cus.name"/>】周菜单要求汇总</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
        <div class="modal-body">
          <table class="table">
              <thead>
                <tr>
                  <th scope="col"></th>
                    <th scope="col">要求</th>
                    <th scope="col">实际频次</th>
                </tr>
              </thead>
              <tbody>
                <tr t-att-class="state.result.is_cow ? '':'text-danger'">
                  <th scope="row">牛肉</th>
                    <td><t t-esc="state.cus.cow_freq"/></td>
                    <td><t t-esc="state.current.cow"/></td>
                </tr>
                  <tr t-att-class="state.result.is_shrimp ? '':'text-danger'">
                  <th scope="row">虾</th>
                      <td><t t-esc="state.cus.shrimp_freq"/></td>
                      <td><t t-esc="state.current.shrimp"/></td>
                </tr>
                  <tr t-att-class="state.result.is_fish ? '':'text-danger'">
                  <th scope="row">鱼</th>
                      <td><t t-esc="state.cus.fish_freq"/></td>
                      <td><t t-esc="state.current.fish"/></td>
                </tr>
                  <tr t-att-class="state.result.is_dark_goose ? '':'text-danger'">
                  <th scope="row">鸭鹅</th>
                      <td><t t-esc="state.cus.dark_goose_freq"/></td>
                      <td><t t-esc="state.current.dark_goose"/></td>
                </tr>
              </tbody>
            </table>
      </div>
        <!--<div class="modal-footer">-->
        <!--<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" t-if="props.data.isCancel">-->
        <!--取消-->
        <!--</button>-->
        <!--<button type="button" class="btn btn-primary" t-on-click.stop="confirm">-->
        <!--确定-->
        <!--</button>-->
        <!--</div>-->
    </div>
  </div>
</div>
`;
    static props = {
        is_open: {type: Boolean},
        data: {type: Object}
    };

    setup() {
        this.state = useState({
            cus: {},
            current: {},
            result: {},
            msg: ''
        })
        onMounted(() => {
            let _this = this;
            this.Modal = new bootstrap.Modal('#freqModal', {});

            document.getElementById('freqModal').addEventListener('hidden.bs.modal', event => {
                _this.env.closeFreqModal();
            })
        })

        onWillUpdateProps(nextProps => {
            // console.log(nextProps);
            if (nextProps.is_open) {
                return this.open(nextProps.data);
            }
        });

        this.handleConfirm = _.debounce(this.confirm,300);
    }

    open(data) {
        console.log(data.cus);
        this.state.cus = {
            name: data.cus.plan_day_cus_name,
            cow_freq: this.getValue(data.cus.cow_freq),
            dark_goose_freq: this.getValue(data.cus.dark_goose_freq),
            fish_freq: this.getValue(data.cus.fish_freq),
            shrimp_freq: this.getValue(data.cus.shrimp_freq),
        };
        this.state.current = data.current;
        this.state.result = data.result;
        this.Modal.show();
    }

    confirm() {
        this.Modal.hide();
        if (typeof (this.props.data.onConfirm) == 'function') {
            this.props.data.onConfirm();
        }
    }

    getValue(v) {
        let _str = v.substr(0, 2);
        let _number = Number(v.substr(-1));
        let result = '';
        if (_str == '>=' && _number == 0) {
            result = '不要求'
        } else if (_str == '>=') {
            result = "至少" + _number + '次'
        } else if (_str == '<=' && _number == 0) {
            result = '不吃'
        } else if (_str == '<=') {
            result = '不超过' + _number + '次'
        }
        return result;
    }
}


export {
    FreqModal
}
